package com.jarvis.hospital;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.hospital.common.BaseActivity;
import com.jarvis.hospital.common.ResponseData;
import com.jarvis.hospital.model.LoginModel;
import com.jarvis.hospital.model.OpertionDetailReponseModel;
import com.jarvis.hospital.model.UserInfoModel;
import com.jarvis.hospital.servers.WebService;
import com.jarvis.hospital.util.SharePreferenceKey;
import com.jarvis.hospital.util.SharePreferenceUtils;
import com.jarvis.hospital.util.net.RequestApi;

import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseActivity {
    @BindView(R.id.img_login)
    ImageView img_login;
    @BindView(R.id.rel_on)
    RelativeLayout rel_on;
    @BindView(R.id.tv_class_off)
    TextView tv_class_off;
    @BindView(R.id.tv_task)
    TextView tv_task;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_type)
    TextView tv_type;
    private WebService webService;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        webService = RequestApi.create(WebService.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isLogin()) {
            rel_on.setVisibility(View.VISIBLE);
            img_login.setVisibility(View.GONE);

            UserInfoModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
            tv_name.setText(userModel.getUserName());
            tv_type.setText(userModel.getUserSectionName());

        } else {
            rel_on.setVisibility(View.GONE);
            img_login.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initView() {
        img_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLogin();
            }
        });
        tv_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(OperationListActivity.class);
            }
        });
        tv_class_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLoginOut();
            }
        });
    }
    /**
     * 获取手机IMEI号
     */
    public static String getIMEI(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String imei = telephonyManager.getDeviceId();

        return imei;
    }
    private void doLogin() {
        final String imei=getIMEI(this);
        showDialog("加载中");
        webService.login(imei, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData<LoginModel>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData<LoginModel> loginModelResponseData) {
                        if (loginModelResponseData != null) {
                            int resultCode = loginModelResponseData.getResultCode();
                            if (resultCode == 200) {
                                SharePreferenceUtils.putObject(SharePreferenceKey.LOGIN_MODEL, loginModelResponseData.getData());
                                UserInfoModel userInfo = loginModelResponseData.getData().getUserInfo();
//                                Set<String> tag = new HashSet<String>();
//                                tag.add("8888");
//                                JPushInterface.setTags(MainActivity.this,Integer.parseInt(userInfo.getUserId()),tag);
                                JPushInterface.setAlias(MainActivity.this,Integer.parseInt(userInfo.getUserId()),imei);
                                tv_name.setText(userInfo.getUserName());
                                tv_type.setText(userInfo.getUserSectionName());
                                startActivity(OperationListActivity.class);
                            } else {
                                showTextToast(loginModelResponseData.getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });
    }

    private void doLoginOut() {
        UserInfoModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
        String userId = userModel.getUserId();
        showDialog("加载中");
        webService.loginOut(userId, "0")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData loginModelResponseData) {
                        if (loginModelResponseData != null) {
                            int resultCode = loginModelResponseData.getResultCode();
                            if (resultCode == 200) {
                                UserInfoModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
                                JPushInterface.deleteAlias(MainActivity.this,Integer.parseInt(userModel.getUserId()));
//                                JPushInterface.cleanTags(MainActivity.this,Integer.parseInt(userModel.getUserId()));
                                SharePreferenceUtils.remove(SharePreferenceKey.LOGIN_MODEL);
                                rel_on.setVisibility(View.GONE);
                                img_login.setVisibility(View.VISIBLE);
                            } else {
                                showTextToast(loginModelResponseData.getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });
    }
}
